<?php
/**
 * Created by PhpStorm.
 * User: 秋刀鱼
 * Date: 2018/8/7
 * Time: 16:21
 */
namespace app\models;

use yii\base\Model;
use Yii;
use \common\helps\tools;//使用工具类

class Thesis extends Model
{


    /**
     * title 添加论文
     * @return int
     * @throws \yii\db\Exception
     */
    public function addThesis($create_time,$uid)
    {
        $id = tools::createUuid();
        $field = ['id','create_time','user_id'];
        $batchData = [[$id,$create_time,$uid]];
        $add = Yii::$app->db->createCommand()->batchInsert(tools::databaseName().'.d_thesis', $field, $batchData)->execute();
        if($add){
            return $id;
        }else{
            return 1;
        }
    }

    /**
     * title 保存论文
     * @param $id
     * @param $project_id
     * @param $project_name
     * @param $thesis_id
     * @param $article_name
     * @param $periodical_name
     * @param $create_time
     * @param $realpath
     * @param $author
     * @param $uid
     * @return int
     * @throws \yii\db\Exception
     */
    public function saveThesis($id,$project_id,$project_name,$thesis_id,$article_name,$periodical_name,$update_time,$author,$uid,$status,$pilot_date,$imageFile)
    {
        $data = ['project_id'=>$project_id,'project_name'=>$project_name,'thesis_id'=>$thesis_id,'article_name'=>$article_name,'periodical_name'=>$periodical_name,'update_time'=>$update_time,'author'=>$author,'user_id'=>$uid,'status'=>$status,'pilot_date'=>$pilot_date,'image'=>$imageFile];

        $save = Yii::$app->db->createCommand()->update(tools::databaseName().'.d_thesis', $data, "id = '$id'")->execute();
        return $save;
    }


    /**
     * title 论文列表
     * @param $project_id
     * @return array
     */
    public function thesisList($user_id)
    {
        $query = new \yii\db\Query();
        $list = $query->select('*')->from(tools::databaseName().'.d_thesis')->where(['user_id'=>$user_id])->all();
        foreach($list as $k => $v){
            //论文
            $thesis = $query->select(['content'])->from(tools::databaseName().'.thesis_type')->where(['id'=>$v['thesis_id']])->one();
            $list[$k]['thesis'] = $thesis['content'];

        }

        return $list;


    }

    /**
     * title 论文详情
     * @param $project_id
     * @return array
     */
    public function detailThesis($id)
    {
        $query = new \yii\db\Query();
        $list = $query->select('*')->from(tools::databaseName().'.d_thesis')->where(['id'=>$id])->all();
        $data = [];
        foreach($list as $k => $v){
            //论文
            $thesis = $query->select(['content'])->from(tools::databaseName().'.thesis_type')->where(['id'=>$v['thesis_id']])->one();
            $v['thesis'] = $thesis['content'];
            $data[] = $v;

        }

        return $data;


    }


    /**
     * title 删除论文
     * @param $id
     * @return int
     * @throws \yii\db\Exception
     */
    public function delThesis($id)
    {
        $del = Yii::$app->db->createCommand()->delete(tools::databaseName().'.d_thesis', "id = '$id'")->execute();

        return $del;
    }


    /**
     * title 添加文件详情表
     * @param $file_name
     * @param $file_type
     * @param $businesskey
     * @param $extend
     * @param $create_time
     * @param $user_id
     * @return int
     * @throws \yii\db\Exception
     */
    public function addFileDetail($file_name,$file_type,$businesskey,$extend,$create_time,$user_id)
    {
        $query = new \yii\db\Query();
        $file =  $query->select('id')->from(tools::databaseName().'.d_file_detail')->where(['businesskey'=>$businesskey])->one();
        if($file){
            $del = Yii::$app->db->createCommand()->delete(tools::databaseName().'.d_file_detail', "businesskey = '$businesskey'")->execute();
            if (!$del) {
                return 0;
            }
        }

        $id = tools::createUuid();
        $field = ['id','file_name','file_type','businesskey','extend','create_time','user_id'];
        $batchData = [[$id,$file_name,$file_type,$businesskey,$extend,$create_time,$user_id]];
        $add = Yii::$app->db->createCommand()->batchInsert(tools::databaseName().'.d_file_detail', $field, $batchData)->execute();
        return $add;
    }


    /**
     * title 条件搜索
     * @param $uid
     * @param $type
     * @param $status
     * @return array
     */
    public function searchThesis($uid,$type,$status)
    {
        $query = new \yii\db\Query();
        if(empty($type)) {
            $where = ['user_id' => $uid, 'status' => $status];
        }else{
            $where = ['user_id' => $uid, 'thesis_id' => $type, 'status' => $status];
        }
        $list = $query->select('*')->from(tools::databaseName().'.d_thesis')->where($where)->all();
        foreach($list as $k => $v){
            //论文
            $thesis = $query->select(['content'])->from(tools::databaseName().'.thesis_type')->where(['id'=>$v['thesis_id']])->one();
            $list[$k]['thesis'] = $thesis['content'];

        }
        return $list;


    }







}